package com.oa.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.oa.mapper.*;
import com.oa.pageUtils.PaginationContext;
import com.oa.pojo.PageBean;
import com.oa.pojo.PageInfo;
import com.oa.pojo.Permission;
import com.oa.pojo.User;
import com.oa.utils.EntityException;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Author Str
 * @Date 2021/12/15 16:13
 * @Description :
 */
@Controller
@RequestMapping("/test")
public class TestController {
    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private NotifyMapper notifyMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    // /test/getHighDeptId?id=d01
    @RequestMapping(value = "/getHighDeptId", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String saveDept(@RequestParam Map<String, String> data) {
        for (String s : data.keySet()) {
            System.out.println("key:" + s + ",value:" + data.get(s));
        }

        JSONObject jsObj = new JSONObject();
        String respTxt = "";
        String deptId = data.get("id");
        System.out.println("deptId:" + deptId);
        String highDeptId = deptMapper.getHighDeptId(deptId);
        respTxt = jsObj.toString();
        System.out.println("highDeptId:" + highDeptId);
        return respTxt;
    }

    // /test/getUserList
    @RequestMapping(value = "/getUserList", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String getUserList() {
        JSONObject jsObj = new JSONObject();
        String respTxt = "";
        List<User> users = userMapper.queryList(new HashMap());
        jsObj.put("users", users);
        respTxt = jsObj.toString();
        System.out.println("users:" + users);
        return respTxt;
    }

    // /test/getUserByNoAndEmail?pageNum=1&pageSize=1
    @RequestMapping(value = "/getUserByNoAndEmail", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String getUserByNoAndEmail(@RequestParam(name = "pageNum", required = true, defaultValue = "1") int pageNum,
                                      @RequestParam(name = "pageSize", required = true, defaultValue = "10") int pageSize) {

        Map<String, Object> map = new HashMap<>();
        // 条件
        map.put("sex", "男");
        PageHelper.startPage(pageNum, pageSize);
        List<User> list = userMapper.queryList(map);

        //分页时，实际返回的结果list类型是Page<E>
        PageInfo<User> info = new PageInfo<>(list);

        JSONObject jo = new JSONObject();
        jo.put("data", info);
        return jo.toJSONString();
    }

    // /test/getNotifyList
    @RequestMapping(value = "/getNotifyList", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String getNotifyList() {


        JSONObject jo = new JSONObject();
        jo.put("data", notifyMapper.queryList(null));
        return jo.toJSONString();
    }

    // /test/getFileName?notifyId=4fbeb3e4e0db4c62a6352a8fdb21f99e
    @RequestMapping(value = "/getFileName", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String getFileName(@RequestParam("notifyId") String notifyId) {
        String fileNameById = notifyMapper.getFileNameById(notifyId);
        return fileNameById;
    }

    // /test/updateHasHub?hasHub=1&parentId=d11
    @RequestMapping(value = "/updateHasHub", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String updateHasHub(@RequestParam("parentId") String parentId, @RequestParam("hasHub") String hasHub) {
        int i = deptMapper.updateHasSub(parentId, hasHub);
        return i + "";
    }

    // /test/getRolePermission?roleId=r01
    @RequestMapping(value = "/getRolePermission", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String getRolePermission(@RequestParam("roleId") String roleId) {
        System.out.println("roleId:" + roleId);
        Set<String> permissions = roleMapper.getRolePermission(roleId);
        System.out.println("=====================映射地址集合===================");
        System.out.println(permissions);
        System.out.println("=====================映射地址集合===================");
        return permissions.toString();
    }


    // /test/getSysPermission
    @RequestMapping(value = "/getSysPermission", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String getSysPermission() {
        Set<Permission> permissions = permissionMapper.getSysPermission();
        System.out.println("=====================映射地址、权限集合===================");
        System.out.println(permissions);
        System.out.println("=====================映射地址、权限集合===================");
        return permissions.toString();
    }

}
